# todo_api 冒烟测试脚本
import requests
import hashlib
import time

def make_md5(text):
    """
    Md5加密 只能加密字符串
    :param text:  需要加密的内容，必须是个字符串
    :return:
    """
    m = hashlib.md5()  # 创建一个 md5加密工具
    m.update(str(text).encode(encoding='utf-8'))
    res = m.hexdigest()  # 生成加密后的内容
    return res

s = requests.session()
# 注册
regurl = 'http://114.132.200.42:3456/user/register'
username= int(time.time())
mydata = {
    "username":username,
    "password":"admin123",
    "cpassword":"admin123",
    "checkcode":"1234",
    "email":f"{int(time.time())}@qq.com"
}
res = s.post(url=regurl, data=mydata)
print(res.json())


# 登录
url = 'http://114.132.200.42:3456/user/login'
data = {
    "username": "python001",
    "password": make_md5('admin123'),
    "checkcode": "1234"
}
res = s.post(url=url, json=data)  # 发送请求
# print( res.json() )
# print( res.status_code )
result = res.json()
print(result)
# 拿到用户id与token
getuserid = result['userId']
getuserToken = result['userToken']


# 创建任务
CreateUrl = 'http://114.132.200.42:3456/task/info'
mydata = {"title": "我的任务010203"}

# 头部信息 同样也以字典的形式 存储
hd = {"userId": getuserid, "userToken": getuserToken}

res = s.post(url=CreateUrl, json=mydata, headers=hd)  # headers 参数 表示 头部信息
result = res.json()


res2 = res.json()
takid = res2['taskid']
print(takid)



# 查询任务
geturl = 'http://114.132.200.42:3456/task/info'
mycom = {'status': "0"}
res = s.get(url=geturl, params=mycom, headers=hd)
print(res.json())


# 完成任务
url = 'http://114.132.200.42:3456/task/info'
mydata = {'task_id': takid}
res = s.put(url=url, data=mydata, headers=hd)
print(res.json())

# 取消完成任务
url = 'http://114.132.200.42:3456/task/info'
mydata = {'task_id': takid}
res = s.put(url=url, data=mydata, headers=hd)
print(res.json())

# 删除任务
delurl = 'http://114.132.200.42:3456/task/info'
mydel = {'task_id': takid}
res = s.delete(url=delurl, data=mydel,headers=hd)
print(res.json())



# 上传头像
imgurl = 'http://114.132.200.42:3456/user/uploal_action'
myimg = [
    ('pic', ('gtl.png',open('gtl.png','rb') ) )
]
res = s.post(url=imgurl,headers=hd,files=myimg) # 关于文件上传的接口 files 表示
print(res.json())


